package com.changgou.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.pojo.Goods;
import com.changgou.goods.pojo.Spu;
import com.changgou.service.SpuService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author : 残影
 * @Date: 2020/7/29 13:44
 */
@RestController
@CrossOrigin
@RequestMapping("/spu")
@Api(tags = "Spu")
public class SpuController {
    @Autowired
    private SpuService spuService;

    @ApiOperation("查询全部数据")
    @GetMapping
    public Result<Spu> findAll() {
        List<Spu> list = spuService.findAll();
        return new Result(true, StatusCode.OK, "查询成功", list);
    }

    @ApiOperation("根据ID查询数据")
    @GetMapping("/{id}")
    public Result findById(@PathVariable String id){
        Goods goods = spuService.findGoodsById(id);
        return new Result(true,StatusCode.OK,"查询成功",goods);
    }

    @ApiOperation("新增商品数据")
    @PostMapping
    public Result saveGoods(@RequestBody Goods goods) {
        spuService.saveGoods(goods);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    @ApiOperation("修改数据")
    @PutMapping(value = "/{id}")
    public Result update(@RequestBody Goods goods,@PathVariable String id){
        spuService.update(goods);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    @ApiOperation("根据ID删除数据")
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable Long id) {
        spuService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    @ApiOperation("条件分页查询")
    @PostMapping(value = "/search/{page}/{size}")
    public Result findPage(@RequestBody(required = false) @ApiParam(name = "Spu对象", value = "传入JSON数据", required = false) Spu spu, @PathVariable int page, @PathVariable int size) {
        PageInfo<Spu> pageInfo = spuService.findPage(spu, page, size);
        return new Result(true, StatusCode.OK, "查询成功", pageInfo);
    }

    @ApiOperation("分页搜索实现")
    @GetMapping(value = "/search/{page}/{size}")
    public Result findPage(@PathVariable int page, @PathVariable int size) {
        PageInfo<Spu> pageInfo = spuService.findPage(page, size);
        return new Result(true, StatusCode.OK, "查询成功", pageInfo);
    }

    @ApiOperation("多条件搜索品牌数据")
    @PostMapping(value = "/search")
    public Result findList(@RequestBody(required = false) @ApiParam(name = "Spu对象", value = "传入JSON数据", required = false) Spu spu) {
        List<Spu> list = spuService.findList(spu);
        return new Result(true, StatusCode.OK, "查询成功", list);
    }

    @ApiOperation("审核")
    @PutMapping("/audit/{id}")
    public Result audit(@PathVariable String id){
        spuService.audit(id);
        return new Result(true, StatusCode.OK, "审核成功");
    }

    @ApiOperation("下架")
    @PutMapping("/pull/{id}")
    public Result pull(@PathVariable String id){
        spuService.pull(id);
        return new Result(true, StatusCode.OK, "下架成功");
    }

    @ApiOperation("上架")
    @PutMapping("/put/{id}")
    public Result put(@PathVariable String id){
        spuService.put(id);
        return new Result(true, StatusCode.OK, "上架成功");
    }

    @ApiOperation("恢复数据")
    @PutMapping("/restore/{id}")
    public Result restore(@PathVariable String id){
        spuService.restore(id);
        return new Result();
    }

}
